<?php
include('config.php');

session_start();

if (!session_is_registered("myusername"))
{
	echo "Please log out and log back in.";
}

?>
	        <form id="faultySTBForm" action="" method="GET">
                <table>
                        <tr><td>Serial Number:</td><td><input onChange="stbWarranty();" class="input" id="stbSerial" name="serial"></input>  *Model number is derived from serial number<span id="stbWarranty"></span></td></tr>
	                <tr><td valign="top">Problem Description:</td><td><textarea id="problemDescription" rows="30" cols="50"></textarea></td></tr>        
			<tr><td></td><td><input type="button" id="stbSubmit" value="Send!" onClick="MochaUI.notification('Please Wait...')"/><input type="reset" value="cancel"/></td></tr>

                </table>
		</form>
<script type="text/javascript">

function stbWarranty() {
	var curDate = new Date();
	var serial = document.getElementById('stbSerial').value;
	var model = serial.substr(0,3);
	var manfWeekMS = serial.substr(7,2) * 604800000;
	var manfDate = "January 1, 200"+serial.substr(6,1);
	var manfYearMS = Date.parse(manfDate);
	var yearMS = 31536000000;
	var warrantyPeriod = yearMS * 1;
	var manfTotalMS = manfWeekMS + manfYearMS + warrantyPeriod;

		model = model.toUpperCase();
		if (model == "BZX") {
			model = "ADB2500";
		}
		else {
			model = "";
		}
		
		if (model != "") {
			if (manfTotalMS > Date.parse(curDate)) {
				document.getElementById('stbWarranty').innerHTML = "Device is in warranty"; 
				document.getElementById('stbWarranty').style.color = "green";
        			//  Add Device to DB
				$('stbSubmit').addEvent('click', function(e){
					//MochaUI.notification('Device Submitted');
                			var url = 'pages/query.php?';
					url=url+'serial='+$('stbSerial').value;
					url=url+'&problemDescription='+$('problemDescription').value;
					url=url+'&cmd=faultySTB';
					url=url+'&nocache='+Math.random();
					MochaUI.updateContent({
						'element': $('panel1'),
						'loadMethod': 'xhr',
						'url': url,
						'title': 'Command Response',
						'padding': { top: 8, right: 8, bottom: 8, left: 8 }
					});
        				showSpinner();
				});
			}
			else {
				document.getElementById('stbWarranty').innerHTML = "Device is out of warranty"; 
				document.getElementById('stbWarranty').style.color = "red";
			}
		}
		else {
			document.getElementById('stbWarranty').innerHTML = "Invalid Serial";
			document.getElementById('stbWarranty').style.color = "red";
		}	
	
	//alert("Current Date: "+Date.parse(curDate)+"\n"+"Manf Date: "+manfTotalMS+"\nModel: "+model+"\nWeek: "+manfWeek);
	
}

</script>
